package cn.fscode.code.generator.controller;

import cn.fscode.code.generator.manager.WordTableManager;
import cn.fscode.code.generator.model.req.GenTableInfoSaveReq;
import cn.fscode.code.generator.model.req.GenTablePageReq;
import cn.fscode.code.generator.model.req.ImportDbTableReq;
import cn.fscode.code.generator.model.req.ImportDocumentTableReq;
import cn.fscode.code.generator.model.resp.*;
import cn.fscode.code.generator.service.GenTableColumnService;
import cn.fscode.code.generator.service.GenTableIndexService;
import cn.fscode.code.generator.service.IGenTableService;
import cn.fscode.commons.core.response.Resp;
import cn.fscode.commons.tool.core.exception.BizException;
import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Collections;
import java.util.List;

/**
 * 代码生成器
 * @author shenguangyang
 */
@Validated
@RestController
@RequestMapping("/api/genTable")
public class GenTableController {
    @Resource
    private IGenTableService genTableService;
    @Resource
    private GenTableIndexService genTableIndexService;

    @Resource
    private GenTableColumnService genTableColumnService;

    /**
     * 查询代码生成列表
     */
    @GetMapping("/pageGenTable")
    public Resp<IPage<GenTableResp>> pageGenTable(@Validated GenTablePageReq req) {
        IPage<GenTableResp> resp = genTableService.pageGenTable(req);
        return Resp.ok(resp);
    }

    @GetMapping(value = "/getGenTable")
    public Resp<GenTableResp> getGenTableByTableId(@RequestParam("tableId") Long tableId) {
        GenTableResp resp = genTableService.getGenTableRespByTableId(tableId);
        return Resp.ok(resp);
    }

    @GetMapping("/previewCode")
    public Resp<?> previewCode() {
        System.out.println(genTableService.previewCode(Collections.singletonList(22L))
                .values().stream().findFirst().orElse(""));
        return Resp.ok();
    }

    @PostMapping("/importDocumentTable")
    public Resp<List<ImportTableResp>> importDocumentTable(@RequestBody ImportDocumentTableReq req) {
        if (!FileUtil.exist(req.getFilePath())) {
            throw new BizException("请选择文件");
        }
        WordTableManager wordTableManager = WordTableManager.getInstance(req.getFilePath());
        List<ImportTableResp> respList = genTableService.importWordTable(req, wordTableManager);
        return Resp.ok(respList);
    }

    @PostMapping("/importDbTable")
    public Resp<List<ImportTableResp>> importDbTable(@RequestBody @Validated ImportDbTableReq req) {
        List<ImportTableResp> respList = genTableService.importDbTable(req);
        return Resp.ok(respList);
    }

    @GetMapping("/listGenTableIndex")
    public Resp<List<GenTableIndexResp>> listGenTableIndex(@RequestParam("tableId") Long tableId) {
        List<GenTableIndexResp> resp = genTableIndexService.listGenTableIndexByTableId(tableId);
        return Resp.ok(resp);
    }

    @DeleteMapping("/batchDeleteGenTableIndex")
    public Resp<?> batchDeleteGenTableIndex(@RequestParam("ids") List<Long> ids) {
        genTableIndexService.deleteGenTableIndexByIds(ids);
        return Resp.ok();
    }

    @DeleteMapping("/batchDeleteGenTableColumn")
    public Resp<?> batchDeleteGenTableColumn(@RequestParam("ids") List<Long> ids) {
        genTableColumnService.deleteGenTableColumnByIds(ids);
        return Resp.ok();
    }

    /**
     * 删除代码生成
     */
    @DeleteMapping("/batchDeleteGenTable")
    public Resp<?> deleteGenTableByIds(@RequestParam(value = "ids") List<Long> tableIds) {
        genTableService.deleteGenTableByIds(tableIds);
        return Resp.ok();
    }

    @DeleteMapping("/deleteGenTable")
    public Resp<?> deleteGenTableById(@RequestParam(value = "id") Long id) {
        genTableService.deleteGenTableByIds(Collections.singletonList(id));
        return Resp.ok();
    }

    @GetMapping("/listGenTableColumn")
    public Resp<List<GenTableColumnResp>> listGenTableColumn(@RequestParam("tableId") Long tableId) {
        List<GenTableColumnResp> resp = genTableColumnService.listGenTableColumnByTableId(tableId);
        return Resp.ok(resp);
    }

    @PostMapping("/saveOrUpdateGenTableInfo")
    public Resp<?> saveOrUpdateGenTableInfo(@RequestBody @Validated GenTableInfoSaveReq req) {
        genTableService.saveOrUpdateGenTableInfo(req);
        return Resp.ok();
    }

    @GetMapping("/getGenInfo")
    public Resp<GenInfoResp> getGenInfo(@RequestParam(value = "tableId", required = false)
                                            @NotNull(message = "请选择一张表") Long tableId) {
        GenInfoResp resp = genTableService.getGenInfo(tableId);
        return Resp.ok(resp);
    }
}
